package info.motteke.qjdbc.column.variable;

import static java.util.Calendar.*;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Calendar;

public final class DateVariable extends Variable {

    private long date;

    private boolean set = false;

    public DateVariable(){

    }

    public DateVariable(java.util.Date date){
        is(date);
    }

    public void is(java.util.Date date){
        Calendar cal = getInstance();

        cal.setTime(date);
        cal.set(HOUR, 0);
        cal.set(MINUTE, 0);
        cal.set(SECOND, 0);
        cal.set(MILLISECOND, 0);

        this.date = cal.getTimeInMillis();
        set = true;
    }

    @Override
    void putValue(PreparedStatement pstmt, int index) throws SQLException {
        if(!set){
            throw new IllegalStateException();
        }

        pstmt.setDate(index, new java.sql.Date(date));
    }

}
